<template>
  <div>
    <!-- <h2>账号列表：</h2> -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="_id" label="账号ID" align="center">
      </el-table-column>
      <el-table-column prop="account" label="账号名称" align="center">
      </el-table-column>
      <el-table-column label="用户头像" align="center">
        <template slot-scope="{ row }">
          <img class="avatar" :src="row.file" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="账号类型" align="center">
        <template slot-scope="{ row }">
          <div>
            <el-tag v-if="row.type == '1'" :color="$randomColor()"
              >普通用户</el-tag
            >
            <el-tag v-else :color="$randomColor()">超级管理员</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center">
        <template slot-scope="{ row }">
          <div>
            <el-tag v-if="row.power == '1'" :color="$randomColor()"
              >正常</el-tag
            >
            <el-tag v-else :color="$randomColor()">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            size="small"
            @click="$router.push(`/accountEdit/${row._id}`)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="editAccount(row._id, row.power)"
            >{{ row.power == 1 ? "禁用" : "启用" }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="getDelete(row.account, row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box">
      <el-pagination
        @current-change="handleCurrentChange"
        background
        layout="total, prev, pager, next"
        :total="this.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getAccountlist,
  getAccountforbid,
  getAccountDelete,
} from "@/api/index.js";
export default {
  data() {
    return {
      loading: true,
      tableData: [],
      total: 0,
      page: 1,
    };
  },
  mounted() {
    this.getUserlist();
  },
  methods: {
    //获取数据的列表
    getUserlist() {
      getAccountlist().then((res) => {
        console.log(res);
        this.tableData = res.data.items;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    //禁用账号
    editAccount(id, power) {
      console.log(id, power);
      if (power == "1") {
        getAccountforbid(id, { power: "2" }).then((res) => {
          if (res.data.status == 200) {
            this.$message.success(res.data.msg);
            this.getUserlist();
          }
        });
      } else {
        getAccountforbid(id, { power: "1" }).then((res) => {
          if (res.data.status == 200) {
            this.$message.success(res.data.msg);
            this.getUserlist();
          }
        });
      }
    },
    getDelete(account, id) {
      this.$confirm(`此操作将永久删除 ${account}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getAccountDelete(id).then((res) => {
            if (res.data.status == 200) {
              this.$message.success(res.data.msg);
              this.getUserlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页
    handleCurrentChange(val) {
      console.log(val);
      this.page = val;
      this.axios
        .get(`/rest/admin_user?page=${this.page}&size=${10}`)
        .then((res) => {
          this.tableData = res.data.items;
          this.total = res.data.total;
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb-box {
  margin: 10px;
}
.avatar {
  width: 60px;
  height: 60px;
}
</style>
